package org.jgs1904.rabbitmq.confirm;

import com.rabbitmq.client.Channel;
import org.jgs1904.utils.RabbitMqUtils;

/**
 * @author: 默苍璃
 * @date: 2023-05-31 17:59
 * <p>
 * 单个确认发布
 */
public class Task01 {

    private static final String QUEUE_NAME = "Task01";
    private static final int MESSAGE_COUNT = 1000;

    public static void main(String[] args) throws Exception {

        try (Channel channel = RabbitMqUtils.getChannel();) {

            // 生成一个队列
            channel.queueDeclare(QUEUE_NAME, false, false, false, null);

            //开启发布确认
            channel.confirmSelect();

            long begin = System.currentTimeMillis();
            for (int i = 0; i < MESSAGE_COUNT; i++) {

                String message = i + "";
                channel.basicPublish("", QUEUE_NAME, null, message.getBytes());

                //服务端返回 false 或超时时间内未返回，生产者可以消息重发
                boolean flag = channel.waitForConfirms();
                if (flag) {
                    System.out.println("消息发送成功");
                }
            }
            long end = System.currentTimeMillis();
            System.out.println("发布" + MESSAGE_COUNT + "个单独确认消息,耗时" + (end - begin) + "ms");
        }
    }

}
